package com.jrh.web.member;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * 用户工具类
 * 
 * @author jasen.hsu.cn@gmail.com 2012-7-17 下午3:31:09
 */
public class UserUtil {

    public static final String ADMIN_USER_SESSION_KEY = "session_store_admin_user";

    /**
     * 存储admin用户信息到session
     * 
     * @param request
     * @param username
     */
    public static void storeUserSession(HttpServletRequest request, String username) {
        request.getSession(true).setAttribute(ADMIN_USER_SESSION_KEY, username);
    }

    /**
     * 清除admin用户session信息
     * 
     * @param request
     */
    public static void clearUserSession(HttpServletRequest request) {
        request.getSession(true).removeAttribute(ADMIN_USER_SESSION_KEY);
    }

    /**
     * 是否已登录
     * 
     * @param request
     * @return
     */
    public static boolean isLogon(HttpServletRequest request) {
        return StringUtils.isNotEmpty((String) request.getSession(true).getAttribute(ADMIN_USER_SESSION_KEY));
    }

    /**
     * 取得当前登录admin用户名
     * 
     * @param request
     * @return
     */
    public String getAdminUser(HttpServletRequest request) {
        return (String) request.getSession(true).getAttribute(ADMIN_USER_SESSION_KEY);
    }
}
